#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from lxml import etree
import openpyxl


xml_path = r'C:\Users\xiaoyao\Desktop\TimeShopConfig.xml'
excel_path = r'C:\Users\xiaoyao\Desktop\TimeShopConfig.xlsx'

wb = openpyxl.load_workbook(excel_path)
ws = wb['Sheet1']
xml_tree = etree.parse(open(xml_path, 'r', encoding='utf-8'))
xml_item_list = xml_tree.getroot()

keys_list = ['时效道具id','时效期限(秒数)','货币类型','价格','show值','折后价格']

column = 1
for key in keys_list:
    ws.cell(row=1, column=column).value = key
    column += 1

row = 2
for item in xml_item_list:
    if 'Buylist' in item.attrib:
        value_list = item.attrib['Buylist']
        if ',' in value_list:
            for value in value_list.split(','):
                column = 1
                sub_value_list = value.split(':')
                for _value in sub_value_list:
                    ws.cell(row=row, column=column).value = _value
                    column += 1
                row += 1
        else:
            column = 1
            sub_value_list = value_list.split(':')
            for _value in sub_value_list:
                ws.cell(row=row, column=column).value = _value
                column += 1
            row += 1

wb.save(excel_path)
